package com.hm.health.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.hm.health.R;
import com.hm.health.View.ECGChartView;
import com.hm.health.records.ecgpatch.EcgPatchRecordReader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EcgFlashExportAdapter extends RecyclerView.Adapter<EcgFlashExportAdapter.ViewHolder> {
    private String TAG = EcgFlashExportAdapter.class.getSimpleName();

    private ArrayList<Integer> mDatas = new ArrayList<>();
    private List<Integer> mLeadState = new ArrayList<>();
    private int mFreq;
    private int mDuration;
    private long mFirstTimestamp;
    private Context mContext;

    public EcgFlashExportAdapter(Context context) {
        mContext = context;
    }

    public void updateData(EcgPatchRecordReader.CurrentHourData currentHourData) {
        // insert data to mDatas by currentHourData.mTimestamps and mLeadState
        currentHourData.insertBlankByLead();

        mFreq = currentHourData.mFrequency;
        mDatas = (ArrayList<Integer>) currentHourData.mDatas;
        mLeadState = currentHourData.mLeadState;
        if (mFreq != 0) {
            if (mDatas.size() / mFreq > 120) {
                mDuration = 10;
            } else if (mDatas.size() / mFreq > 30) {
                mDuration = 10;
            } else {
                mDuration = 5;
            }
        }
        mFirstTimestamp = currentHourData.mStartTimestamp;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_ecgflash_export, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mDatas == null || mDatas.isEmpty() || mContext == null) {
            return;
        }

        // ECG图像会分成多段进行绘制，prevLeadState表示前一个lead状态
        int prevLeadState = -1;
        for (int j=position * mDuration * mFreq; j>=0; j--) {
            if (j < mLeadState.size()) {
                int lead = mLeadState.get(j);
                if (lead == 1 || lead == 2) {
                    // 1: lead on; 2: lead off
                    prevLeadState = lead;
                    break;
                }
            }
        }

        holder.ecg_view.clearData();
        for (int i = position * mDuration * mFreq; i < (position + 1) * mDuration * mFreq; ++i) {
            float v = ECGChartView.INVALID_ECG;

            if (i < mDatas.size()) {
                v = mDatas.get(i) / 100f;
                v = v > 2f ? 2f : v;
                v = v < -1.5f ? -1.5f : v;
            }

            int lead = 0;
            if (prevLeadState == -1) {
                if (i < mLeadState.size()) {
                    lead = mLeadState.get(i);
                }
            } else {
                Log.d(TAG, "Prev lead state of " + position + " is " + prevLeadState);
                lead = prevLeadState;
                prevLeadState = -1;
            }

            holder.ecg_view.addPointWithFlag(v, i + 1, lead);
        }

        //holder.ecg_view.invalidate();
    }

    @Override
    public int getItemCount() {
        if (mDatas == null || mDatas.isEmpty() || mContext == null) {
            return 0;
        }

        if (mFreq == 0) {
            return 0;
        }
        return (mDatas.size() / (mFreq * mDuration)) + (mDatas.size() % (mFreq * mDuration) == 0 ? 0 : 1) ;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ecg_view)
        ECGChartView ecg_view;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            ecg_view.setFrequency(mDuration, mFreq);
            ecg_view.setLineWidth(1);
            ecg_view.setShowFormatTime(true);
            ecg_view.setFirstTimestamp(mFirstTimestamp);
        }
    }
}
